﻿using System;
using System.Collections.Generic;

namespace ProductCollector.Web
{
    public partial class Default : System.Web.UI.Page {
        private const string SELECT_SITE = "--Select Site--";
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                List<string> siteNames = new List<string> { SELECT_SITE };
                siteNames.AddRange(SitesName.GetAllSiteNames());
                m_lstSites.DataSource = siteNames;
                m_lstSites.DataBind();
            }
        }

        protected void m_lstSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_lstSites.SelectedIndex > 0) {
                m_lstCategories.DataSource = SitesName.GetCategoryForSite(m_lstSites.SelectedValue);
                m_lstCategories.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ISiteSearcher searcher = SiteSearcherFactory.CreateSearcher(m_lstSites.SelectedValue);
            if (m_lstSites.SelectedIndex == 0)
                return;
            var searchCriteria=new SearchCriteria
                {
                    Category = m_lstCategories.SelectedValue,
                    SearchValue = TextBox1.Text.Trim(),
                    CurrentPageIndex = 1,
                    AllCategory = m_lstCategories.SelectedValue == null
                };
            var productList= searcher.Search(searchCriteria);
            DataList1.DataSource = productList;
            DataList1.DataBind();
        }
        
    }
}